//package com.hzw.icane.ui.adapter;
//
//import android.content.Context;
//import android.text.TextUtils;
//import android.util.TypedValue;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.widget.ArrayAdapter;
//import android.widget.Filter;
//import android.widget.ImageView;
//import android.widget.RelativeLayout;
//import android.widget.TextView;
//
//import com.hyphenate.chat.EMChatRoom;
//import com.hyphenate.chat.EMClient;
//import com.hyphenate.chat.EMConversation;
//import com.hyphenate.chat.EMGroup;
//import com.hyphenate.chat.EMMessage;
//import com.hyphenate.easeui.controller.EaseUI;
//import com.hyphenate.easeui.model.EaseAtMessageHelper;
//import com.hyphenate.easeui.utils.EaseCommonUtils;
//import com.hyphenate.easeui.utils.EaseSmileUtils;
//import com.hyphenate.easeui.widget.EaseConversationList;
//import com.hyphenate.util.DateUtils;
//import com.hzw.icane.ICaneApplication;
//import com.hzw.icane.R;
//import com.hzw.icane.SOSUserProfileProvider;
//import com.hzw.icane.modle.bean.ease.EaseUser;
//import com.hzw.icane.net.response.LoginRp;
//import com.hzw.icane.utils.CommonUtil;
//import com.hzw.icane.utils.EaseUserUtils;
//import com.hzw.icane.utils.RoundedTransformation;
//import com.squareup.picasso.Picasso;
//
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//
///**
// * conversation list adapter
// */
//public class EaseConversationAdapter extends ArrayAdapter<EMConversation> {
//    private static final String TAG = "ChatAllHistoryAdapter";
//    private List<EMConversation> conversationList;
//    private List<EMConversation> copyConversationList;
//    private ConversationFilter conversationFilter;
//    private boolean notiyfyByFilter;
//
//    protected int primaryColor;
//    protected int secondaryColor;
//    protected int timeColor;
//    protected int primarySize;
//    protected int secondarySize;
//    protected float timeSize;
//
//    public EaseConversationAdapter(Context context, int resource,
//                                   List<EMConversation> objects) {
//        super(context, resource, objects);
//        conversationList = objects;
//        copyConversationList = new ArrayList<EMConversation>();
//        copyConversationList.addAll(objects);
//    }
//
//    @Override
//    public int getCount() {
//        return conversationList.size();
//    }
//
//    @Override
//    public EMConversation getItem(int arg0) {
//        if (arg0 < conversationList.size()) {
//            return conversationList.get(arg0);
//        }
//        return null;
//    }
//
//    @Override
//    public long getItemId(int position) {
//        return position;
//    }
//
//    @Override
//    public View getView(int position, View convertView, ViewGroup parent) {
//        if (convertView == null) {
//            convertView = LayoutInflater.from(getContext()).inflate(R.layout.icane_ease_row_chat_history, parent, false);
//        }
//        ViewHolder holder = (ViewHolder) convertView.getTag();
//        if (holder == null) {
//            holder = new ViewHolder();
//            holder.name = (TextView) convertView.findViewById(R.id.name);
//            holder.unreadLabel = (TextView) convertView.findViewById(R.id.unread_msg_number);
//            holder.message = (TextView) convertView.findViewById(R.id.message);
//            holder.time = (TextView) convertView.findViewById(R.id.time);
//            holder.avatar = (ImageView) convertView.findViewById(R.id.avatar);
//            holder.msgState = convertView.findViewById(R.id.msg_state);
//            holder.list_itease_layout = (RelativeLayout) convertView.findViewById(R.id.list_itease_layout);
//            holder.motioned = (TextView) convertView.findViewById(R.id.mentioned);
//            holder.sosTag = (TextView) convertView.findViewById(R.id.sos_tag);
//            convertView.setTag(holder);
//        }
//        holder.list_itease_layout.setBackgroundResource(R.drawable.ease_mm_listitem);
//
//        // get conversation
//        EMConversation conversation = getItem(position);
//        // get username or group id
//        String username = conversation.conversationId();
//        // @hzy修改
//        if (conversation.getType() == EMConversation.EMConversationType.GroupChat) {
//            String groupId = conversation.conversationId();
//            if (EaseAtMessageHelper.get().hasAtMeMsg(groupId)) {
//                holder.motioned.setVisibility(View.VISIBLE);
//            } else {
//                holder.motioned.setVisibility(View.GONE);
//            }
//            ICaneApplication iCaneApplication = ((ICaneApplication) getContext().getApplicationContext());
//            LoginRp loginRp = iCaneApplication.getmLoginRp();
//            String url = null;
//            String name = null;
//            if (TextUtils.equals(loginRp.groupId, groupId)) {
//                holder.sosTag.setVisibility(View.VISIBLE);
//                name = "我的SOS";
//                url = CommonUtil.utf8Togb2312(loginRp.headImg);
//            } else {
//                holder.sosTag.setVisibility(View.VISIBLE);
//                EaseUser users = SOSUserProfileProvider.getInstance().queryUserByGroupId(getContext(),groupId);
//                if (users == null) {
//                    name = "未知";
//                } else {
//                    name = users.getNick() + "的SOS";
//                }
//            }
//            holder.name.setText(name);
//            if (TextUtils.isEmpty(url)) {
//                EaseUserUtils.setUserAvatar(getContext(), username, holder.avatar);
//            } else {
//                Picasso.with(getContext()).load(url).transform(new RoundedTransformation(9, 0)).into(holder.avatar);
//            }
//        } else if (conversation.getType() == EMConversation.EMConversationType.ChatRoom) {
//            holder.avatar.setImageResource(R.drawable.ease_group_icon);
//            EMChatRoom room = EMClient.getInstance().chatroomManager().getChatRoom(username);
//            holder.name.setText(room != null && !TextUtils.isEmpty(room.getName()) ? room.getName() : username);
//            holder.motioned.setVisibility(View.GONE);
//            holder.sosTag.setVisibility(View.GONE);
//        } else {
//            EaseUserUtils.setUserAvatar(getContext(), username, holder.avatar);
//            EaseUserUtils.setUserNick(username, holder.name);
//            holder.motioned.setVisibility(View.GONE);
//            holder.sosTag.setVisibility(View.GONE);
//        }
//
//        if (conversation.getUnreadMsgCount() > 0) {
//            // show unread message count
//            holder.unreadLabel.setText(String.valueOf(conversation.getUnreadMsgCount()));
//            holder.unreadLabel.setVisibility(View.VISIBLE);
//        } else {
//            holder.unreadLabel.setVisibility(View.INVISIBLE);
//        }
//
//        if (conversation.getAllMsgCount() != 0) {
//            // show the content of latest message
//            EMMessage lastMessage = conversation.getLastMessage();
//            String content = null;
//            if (cvsListHelper != null) {
//                content = cvsListHelper.onSetItemSecondaryText(lastMessage);
//            }
//            holder.message.setText(EaseSmileUtils.getSmiledText(getContext(), EaseCommonUtils.getMessageDigest(lastMessage, (this.getContext()))),
//                    TextView.BufferType.SPANNABLE);
//            if (content != null) {
//                holder.message.setText(content);
//            }
//            holder.time.setText(DateUtils.getTimestampString(new Date(lastMessage.getMsgTime())));
//            if (lastMessage.direct() == EMMessage.Direct.SEND && lastMessage.status() == EMMessage.Status.FAIL) {
//                holder.msgState.setVisibility(View.VISIBLE);
//            } else {
//                holder.msgState.setVisibility(View.GONE);
//            }
//        }
//
//        //set property
//        holder.name.setTextColor(primaryColor);
//        holder.message.setTextColor(secondaryColor);
//        holder.time.setTextColor(timeColor);
//        if (primarySize != 0)
//            holder.name.setTextSize(TypedValue.COMPLEX_UNIT_PX, primarySize);
//        if (secondarySize != 0)
//            holder.message.setTextSize(TypedValue.COMPLEX_UNIT_PX, secondarySize);
//        if (timeSize != 0)
//            holder.time.setTextSize(TypedValue.COMPLEX_UNIT_PX, timeSize);
//
//        return convertView;
//    }
//
//    @Override
//    public void notifyDataSetChanged() {
//        super.notifyDataSetChanged();
//        if (!notiyfyByFilter) {
//            copyConversationList.clear();
//            copyConversationList.addAll(conversationList);
//            notiyfyByFilter = false;
//        }
//    }
//
//    @Override
//    public Filter getFilter() {
//        if (conversationFilter == null) {
//            conversationFilter = new ConversationFilter(conversationList);
//        }
//        return conversationFilter;
//    }
//
//
//    public void setPrimaryColor(int primaryColor) {
//        this.primaryColor = primaryColor;
//    }
//
//    public void setSecondaryColor(int secondaryColor) {
//        this.secondaryColor = secondaryColor;
//    }
//
//    public void setTimeColor(int timeColor) {
//        this.timeColor = timeColor;
//    }
//
//    public void setPrimarySize(int primarySize) {
//        this.primarySize = primarySize;
//    }
//
//    public void setSecondarySize(int secondarySize) {
//        this.secondarySize = secondarySize;
//    }
//
//    public void setTimeSize(float timeSize) {
//        this.timeSize = timeSize;
//    }
//
//
//    private class ConversationFilter extends Filter {
//        List<EMConversation> mOriginalValues = null;
//
//        public ConversationFilter(List<EMConversation> mList) {
//            mOriginalValues = mList;
//        }
//
//        @Override
//        protected FilterResults performFiltering(CharSequence prefix) {
//            FilterResults results = new FilterResults();
//
//            if (mOriginalValues == null) {
//                mOriginalValues = new ArrayList<EMConversation>();
//            }
//            if (prefix == null || prefix.length() == 0) {
//                results.values = copyConversationList;
//                results.count = copyConversationList.size();
//            } else {
//                String prefixString = prefix.toString();
//                final int count = mOriginalValues.size();
//                final ArrayList<EMConversation> newValues = new ArrayList<EMConversation>();
//
//                for (int i = 0; i < count; i++) {
//                    final EMConversation value = mOriginalValues.get(i);
//                    String username = value.conversationId();
//
//                    EMGroup group = EMClient.getInstance().groupManager().getGroup(username);
//                    if (group != null) {
//                        username = group.getGroupName();
//                    } else {
//                        EaseUser user = EaseUserUtils.getUserInfo(username);
//                        // TODO: not support Nick anymore
////                        if(user != null && user.getNick() != null)
////                            username = user.getNick();
//                    }
//
//                    // First match against the whole ,non-splitted value
//                    if (username.startsWith(prefixString)) {
//                        newValues.add(value);
//                    } else {
//                        final String[] words = username.split(" ");
//                        final int wordCount = words.length;
//
//                        // Start at index 0, in case valueText starts with space(s)
//                        for (String word : words) {
//                            if (word.startsWith(prefixString)) {
//                                newValues.add(value);
//                                break;
//                            }
//                        }
//                    }
//                }
//
//                results.values = newValues;
//                results.count = newValues.size();
//            }
//            return results;
//        }
//
//        @Override
//        protected void publishResults(CharSequence constraint, FilterResults results) {
//            conversationList.clear();
//            if (results.values != null) {
//                conversationList.addAll((List<EMConversation>) results.values);
//            }
//            if (results.count > 0) {
//                notiyfyByFilter = true;
//                notifyDataSetChanged();
//            } else {
//                notifyDataSetInvalidated();
//            }
//        }
//    }
//
//    private EaseConversationList.EaseConversationListHelper cvsListHelper;
//
//    public void setCvsListHelper(EaseConversationList.EaseConversationListHelper cvsListHelper) {
//        this.cvsListHelper = cvsListHelper;
//    }
//
//    private static class ViewHolder {
//        /**
//         * who you chat with
//         */
//        TextView name;
//        /**
//         * unread message count
//         */
//        TextView unreadLabel;
//        /**
//         * content of last message
//         */
//        TextView message;
//        /**
//         * time of last message
//         */
//        TextView time;
//        /**
//         * avatar
//         */
//        ImageView avatar;
//        /**
//         * status of last message
//         */
//        View msgState;
//        /**
//         * layout
//         */
//        RelativeLayout list_itease_layout;
//        TextView motioned;
//        TextView sosTag;//我添加
//    }
//}
//
